package com.prizedraw.prize_draw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prizedraw.prize_draw.common.dto.SysMenuDto;
import com.prizedraw.prize_draw.entity.SysMenu;
import com.prizedraw.prize_draw.entity.SysUser;
import com.prizedraw.prize_draw.mapper.SysMenuMapper;
import com.prizedraw.prize_draw.service.SysMenuService;
import com.prizedraw.prize_draw.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czj666
 * @since 2021-09-07
 */
@Slf4j
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    SysUserService sysUserService;

    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        // 这里原来已经根据用户的权限构成了三层，目录-菜单-权限，在动态菜单的加载只读取第二层进行数据渲染
        // 存的是工号，取出来也自然是员工表的user_id，这里不要误会，不是主键id
        String userId = SecurityContextHolder.getContext().getAuthentication().getName();
        SysUser sysUser = sysUserService.getByUserId(userId);
        List<SysMenu> menus = this.list(new QueryWrapper<SysMenu>().inSql("id", "select menu_id from sys_user_menu where user_id = " + sysUser.getId()));
        //把所有的菜单实体转化成树状结构的ArrayList
        List<SysMenu> menuTree = buildTreeMenu(menus);

        //把ArrayList转成DTO
        return convertDTO(menuTree);
    }

    @Override
    public List<SysMenu> tree() {
        // 根据定义的权限排序规则，进行显示
        List<SysMenu> sysMenus = buildTreeMenu(this.list(new QueryWrapper<SysMenu>().orderByAsc("orderNum")));
        return buildTreeMenu(sysMenus);
    }

    /**
     * 把实体转化vue的路由对象，菜单对应的属性，对应好属性名
     * @param menuTree
     * @return
     */
    private List<SysMenuDto> convertDTO(List<SysMenu> menuTree) {
        List<SysMenuDto> sysMenuDtos = new ArrayList<>();
        menuTree.forEach(m -> {
            SysMenuDto sysMenuDto = new SysMenuDto();
            sysMenuDto.setId(m.getId());
            sysMenuDto.setName(m.getPermission());
            sysMenuDto.setTitle(m.getName());
            sysMenuDto.setComponent(m.getComponent());
            sysMenuDto.setPath(m.getPath());
            sysMenuDto.setIcon(m.getIcon());

            if (m.getChildren().size() > 0){
                //如果有子级菜单则继续转换
                sysMenuDto.setChildren(convertDTO(m.getChildren()));
            }
            sysMenuDtos.add(sysMenuDto);
        });
        return sysMenuDtos;
    }

    /**
     * 转化成树状结构逻辑
     * @param menus
     * @return
     */
    private List<SysMenu> buildTreeMenu(List<SysMenu> menus) {
        //定义最高父级菜单
        List<SysMenu> finalMenu = new ArrayList<>();

        //先各自寻找自己的子级菜单
        for (SysMenu menu : menus) {
            for (SysMenu e : menus) {
                if (e.getParentId() == menu.getId()){
                    menu.getChildren().add(e);
                }
            }

            //该菜单没有父级
            //czj666
            if (menu.getParentId() == 0){
                finalMenu.add(menu);
            }
        }

        log.info("菜单实体转化成树状结构----{}",finalMenu);
        return finalMenu;
    }
}
